package com.mocha.bss.domain.org.contoller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coffee.attachment.excel.ExcelResponseEnum;
import com.coffee.core.CoffeePage;
import com.coffee.core.CoffeeResponse;
import com.coffee.core.CoffeeResponseEnum;
import com.coffee.core.ValidGroup;
import com.coffee.log.capture.OperationLog;
import com.coffee.web.CoffeeRunTimeLogs;
import com.mocha.bss.domain.org.repository.MochaUserRepository;
import com.mocha.bss.domain.rbac.repository.MochaRoleUserRepository;
import com.mocha.bss.feign.bo.UserInfoBO;
import com.mocha.bss.domain.org.contoller.query.UserQuery;
import com.mocha.bss.domain.org.repository.entity.UserEntity;
import com.mocha.bss.org.bo.UserBO;
import com.mocha.bss.org.vo.UserVO;
import com.mocha.bss.rbac.bo.RoleRemoveUserBO;
import com.mocha.bss.rbac.bo.RoleUserBO;
import com.pinghua.mocha.basics.enumerate.MochaSocpEnum;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author songkui
 * @since 2022-02-11
 * @version v2.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final MochaUserRepository userService;
    private final MochaRoleUserRepository roleUserService;

    /**
     * 新增用户
     * @param
     * @return
     */
    @OperationLog(scope = "PC", name = "新增用户")
    @PostMapping("/save")
    public CoffeeResponse<?> saveUser(@Validated({ValidGroup.Create.class}) @RequestBody UserBO userBO) {
        userService.saveUser(userBO);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /**
     * 更新用户
     * @param
     * @return
     */
    @OperationLog(scope = "PC", name = "更新用户")
    @PutMapping("/update")
    public CoffeeResponse<?> updateUser(@Validated(ValidGroup.Update.class) @RequestBody UserBO userBO) {
        userService.updateUser(userBO);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /**
     * 删除用户
     * @param
     * @return
     */
    @OperationLog(scope = "PC", name = "删除用户")
    @DeleteMapping("/delete/{id}")
    public CoffeeResponse<?> deleteById(@PathVariable Integer id) {
        userService.removeById(id);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /**
     * 查找用户列表
     * @return
     */
    @GetMapping("/list")
    public CoffeeResponse<List<UserVO>> list(@RequestBody UserBO userBO){
        List<UserVO> list = userService.listByQuery(userBO);
        return CoffeeResponse.responseSuccess(list);
    }

    /**
     * 分页查询用户列表
     * @param query
     * @return
     */
    @OperationLog(scope = "PC", name = "分页查询用户列表")
    @PostMapping("/page")
    public CoffeeResponse<CoffeePage<UserVO>> page(@RequestBody UserQuery<UserVO> query){
        CoffeePage<UserVO> page = userService.pageByQuery(query);
        return CoffeeResponse.responseSuccess(page);
    }


    /** 获取角色下 用户  */
    @GetMapping("/role/{roleId}")
    public CoffeeResponse<List<UserInfoBO>> listByRoleId(@PathVariable Integer roleId){
       UserQuery<UserInfoBO> userQuery = new UserQuery<>();
       userQuery.setRoleId(roleId);
        return CoffeeResponse.responseSuccess(userService.listByQuery(userQuery));
    }

    /** 用户列表 -- 除开以绑定角色  */
    @GetMapping("/role/except/{roleId}")
    public CoffeeResponse<Set<UserInfoBO>> listByExcept(@PathVariable Integer roleId){
        return CoffeeResponse.responseSuccess(userService.listByExcept(roleId));
    }

    /** 角色 绑定 用户  */
    @OperationLog(scope = "PC", name = "角色-绑定-用户")
    @PostMapping("/role/add-user")
    public CoffeeResponse<?> addUserRole(@RequestBody RoleUserBO userBO){
        roleUserService.addUserRole(userBO);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /** 角色 移除 用户 */
    @OperationLog(scope = "PC", name = "角色-移除-用户")
    @PostMapping("/role/remove-user")
    public CoffeeResponse<?> removeUserRole(@RequestBody RoleRemoveUserBO userBO){
        roleUserService.removeUserRole(userBO);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }


    @GetMapping("/test/find")
    public CoffeeResponse<Page<UserEntity>> find(){
        UserQuery<UserInfoBO> userQuery = new UserQuery();
        userQuery.setUserName("admin");
        Page<UserEntity> page = userService.findByPage(userQuery.buildPage());
        return CoffeeResponse.responseSuccess(page);
    }

    @PostMapping("/test/query")
    public CoffeeResponse<CoffeePage<UserInfoBO>> query(@RequestBody UserQuery<UserInfoBO> query){
        CoffeePage<UserInfoBO> page = userService.findByQuery(query);
        return CoffeeResponse.responseSuccess(page);
    }

    @GetMapping("/test/1")
    public CoffeeResponse<?> test(){
        return CoffeeResponse.response(ExcelResponseEnum.ExcelNotCode);
    }

    @CoffeeRunTimeLogs
    @GetMapping("/test")
    public CoffeeResponse<?> test(HttpServletRequest request){
        log.debug("Controller:  USERID: {}   USERCODE {}  ", request.getHeader("User-id"), request.getHeader("token")  );
        return  CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

}
